// pages/games/games.js
const app = getApp()
const url = app.basicsUrl();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    offredenvelopes: true,
    list: [{
      name: 'Young',
      money: 1.00,
      frequency: 9,
      time: '1月09日 14:37'
    }, {
      name: 'Young',
      money: 1.00,
      frequency: 9,
      time: '1月09日 14:37'
    }, {
      name: 'Young',
      money: 1.00,
      frequency: 9,
      time: '1月09日 14:37'
    }, {
      name: 'Young',
      money: 1.00,
      frequency: 9,
      time: '1月09日 14:37'
    }],
    gameId:"",
    hongBaoHardType:"",
    loginInfo:"",
    hongbaoData:"",
    hasPickMoney:"",
    money: "",
    msg:"",
    failData:"",
    successData:"",
    left:"",//剩余奖励次数
    canPlay: '',
    hongbaomoney: '',
    gamesshow: false,
    wanshow: false,
    moneyshow: false,
    questionType:"",
    transform: '',
    selfHongBao: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var hongbaoId = options.hongBaoId;
    var msg = options.msg;
    var hasPickMoney = options.hasPickMoney;
    var money = options.money;
    var that = this;
    that.setData({
      msg: msg,
      hasPickMoney: hasPickMoney,
      money: money,
      hongbaoId: hongbaoId,
    })
    if (hasPickMoney){
      that.setData({
        offredenvelopes:false,
        transform: 'transform'
      })
    }
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    wx.getStorage({
      key: 'loginInfo',
      success: function(res) {
        var data=res.data;
        that.init(data)
      }
    })
  },

  /** 
   * 点击跳转提现页面
   */
  bindwithdrawals: function () {
    var that = this;
    wx.navigateTo({
      url: '/pages/withdrawals/withdrawals'
    })
  },

  /** 
   * 点击关闭领取奖励box
   */
  bindoffredenvelopes: function () {
    var that = this;
    that.setData({
      offredenvelopes: true
    });
  },

  /** 
   * 点击跳转首页
   */
  bindindex: function () {
    var that = this;
    wx.switchTab({
      url: '/pages/index/index'
    })
  },

  /** 
   * 点击跳转转发页面
   */
  bindforward: function () {
    var that = this;
    var gameId = that.data.gameId;
    var hongBaoHardType = that.data.hongBaoHardType;
    var hongbaoId = that.data.hongbaoId;
    console.log(gameId, hongBaoHardType, hongbaoId)
    wx.navigateTo({
      url: '/pages/forward/forward?gameId=' + gameId + '&hongBaoHardType=' + hongBaoHardType + '&hongbaoId=' + hongbaoId
    })
  },

  /** 
   * 滑动切换tab 
   */
  bindChange: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },

  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },

  /** 
   * 点击进入‘别踩白块’游戏界面
   */
  bindwhitepieces: function () {
    var that = this;
    var gameId = that.data.gameId;
    var hongBaoHardType = that.data.hongBaoHardType;
    var hongbaoId = that.data.hongbaoId;
    var questionType = that.data.questionType;
    var url;
    if (gameId==1){
      url = '/pages/answerGame/answerGame?gameId=' + gameId + '&hongBaoHardType=' + hongBaoHardType + '&hongbaoId=' + hongbaoId + '&questionType=' + questionType; 
    }else if(gameId==2){
      url = '/pages/Unlock/Unlock?gameId=' + gameId + '&hongBaoHardType=' + hongBaoHardType + '&hongbaoId=' + hongbaoId;
    }else if(gameId==3){
      url = '/pages/play/play?gameId=' + gameId + '&hongBaoHardType=' + hongBaoHardType + '&hongbaoId=' + hongbaoId;
    }
    wx.navigateTo({
      url: url
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.wxLogin();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  init(loginInfo) {
    var that=this;
    wx.getStorage({
      key: 'loginInfo',
      success: function(res) {
        var loginInfo=res.data;
        var params = {};
        params['hongBaoId'] = that.data.hongbaoId;
        params['tokenKey'] = loginInfo.id;//奖励描述信息
        params['openId'] = loginInfo.openId;//奖励描述信息
        params['token'] = loginInfo.token;//奖励描述信息
        wx.request({
          url: url + "authorized/hongBao",
          data: params,
          header: {
            'content-type': 'application/json'
          },
          success: res => {
            var data=res.data;
            if(data.code==200){
              data.data.name = decodeURI(data.data.name);
              var hongbaoData=data.data;
              var failData = hongbaoData.fail;
              if (hongbaoData.questionProperty){
                var questionType = hongbaoData.questionProperty.questionType;//问题类型
              }
              for (var i = 0; i < failData.length;i++){
                failData[i]['createTime'] = app.timeFormat(failData[i]['createTime']/1000)
                failData[i]['userName'] = decodeURI((failData[i]['userName']))
              }
              var successData=hongbaoData.success;
              for (var i = 0; i < successData.length; i++) {
                successData[i]['createTime'] = app.timeFormat(successData[i]['createTime'] / 1000)
                successData[i]['userName'] = decodeURI((successData[i]['userName']))
              }
              var hongBaoHardType = hongbaoData.hongBao.hongBaoHardType;
              var gameId = hongbaoData.hongBao.gameId;
              var canPlay = hongbaoData.canPlay;
              var hongbaomoney = Number(hongbaoData.hongBao.money / 100).toFixed(2);
              var left = hongbaoData.left;
              var selfHongBao = hongbaoData.selfHongBao
              that.setData({
                hongbaoData: hongbaoData,
                failData: failData,
                successData: successData,
                gameId: gameId,
                hongBaoHardType: hongBaoHardType,
                canPlay: canPlay,
                hongbaomoney: hongbaomoney,
                left: left,
                questionType: questionType,
                selfHongBao: selfHongBao
              })
            }
            if (that.data.left > 0 && that.data.canPlay == true) {
              that.setData({
                gamesshow: true,
                wanshow: false,
                moneyshow: false,
              });
            } else if (that.data.left == 0 && that.data.canPlay == true) {
              that.setData({
                gamesshow: false,
                wanshow: true,
                moneyshow: false,
              });
            } else if (that.data.canPlay == false) {
              that.setData({
                gamesshow: false,
                wanshow: false,
                moneyshow: true,
              });
            } else {
              that.setData({
                gamesshow: false,
                wanshow: false,
                moneyshow: false,
              });
            }
          }
        })
      }
    })
    
  },
  wxLogin() {
    var that = this;
    wx.login({
      success: res => {
        console.log(res)
        var code = res.code;
        // 获取用户信息
        wx.getSetting({
          success: res => {
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
            wx.getUserInfo({
              success: res => {
                res.userInfo['code'] = code;
                var userInfo = res.userInfo;
                that.login(userInfo);
              },
              fail:res=>{
                if (res.errMsg =="getUserInfo:fail auth deny"){
                  wx.showModal({
                    title: '提示',
                    content: '小程序需要获取用户信息权限，点击确认前往设置或退出程序',
                    showCancel:false,
                    success: function (res) {
                      if (res.confirm) {
                        that.checkLogin();
                      } else if (res.cancel) {
                      }
                    }
                  })
                }
              }
            })
          }
        })
      }
    })
  },
  login(userInfo) {
    var that = this;
    var params = {};
    params['jsCode'] = userInfo.code;
    params['nickname'] = userInfo.nickName;
    params['sex'] = userInfo.gender;
    params['city'] = userInfo.city;
    params['province'] = userInfo.province;
    params['headimgurl'] = userInfo.avatarUrl;
    wx.request({
      url: url + 'login',
      data: params,
      success: res => {
        var loginInfo = res.data.data;
        var nikeName = userInfo.nickName
        loginInfo['nikeName'] = nikeName;
        that.setData({ loginInfo: loginInfo })
        wx.setStorage({
          key: "loginInfo",
          data: loginInfo
        })
        that.init(loginInfo)
      }
    })
  },
  checkLogin(){
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          wx.openSetting({
            success: (res) => {
              if (res['authSetting']['scope.userInfo']) {
                // that.wxLogin();
                
              }
            }
          })
        }
      }
    })
  }
})